import { stringify } from 'querystring';
import { history } from 'umi';
import {
  fakeLoginByPassword,
  fakeLoginByVerification,
  fakeResetPassword,
  fetchListLoginAdminAccess,
  fakeLoginOut,
  fakeSendSmsCodeBack
} from '@/services/LoginServices';
import { notification } from 'antd';
const LoginForm = {
  namespace: 'login',

  state: {
    accountList: [],
    accessList: [],
    bottomAccessList: [],
  },

  effects: {

    *login({ payload, callback }, { call, put }) {
      const response = yield call(fakeLoginByPassword, payload);
      if (!response) return;
      const {
        content: { adminInfo },
      } = response;
      window.localStorage.setItem('account', JSON.stringify(adminInfo));
      window.localStorage.setItem('token', adminInfo.token);
      callback && callback(adminInfo);
    },

    *fakeLoginByVerification({ payload, callback }, { call, put }) {
      const response = yield call(fakeLoginByVerification, payload);
      if (!response) return;
      const {
        content: { adminInfo },
      } = response;
      window.localStorage.setItem('account', JSON.stringify(adminInfo));
      window.localStorage.setItem('token', adminInfo.token);
      callback && callback(adminInfo);
    },
    *fakeSendSmsCodeBack({ payload, callback }, { call, put }) {
      const response = yield call(fakeSendSmsCodeBack, payload);
      if (!response) return;
      callback && callback();
    },
    *fakeResetPassword({ payload, callback }, { call, put }) {
      const response = yield call(fakeResetPassword, payload);
      if (!response) return;
      callback && callback();
    },
    *fakeloginOut({ payload }, { call, put }) {
      if (!window.localStorage.getItem('token')) {
        window.localStorage.removeItem('account');
        window.localStorage.removeItem('token');
        window.localStorage.removeItem('account');
        history.replace({
          pathname: '/login/index',
        });
        return;
      }
      const response = yield call(fakeLoginOut, payload);
      if (!response) return;
      window.localStorage.removeItem('account');
      window.localStorage.removeItem('token');
      window.localStorage.removeItem('account');
      history.replace({
        pathname: '/login/index',
      });
      notification.success({
        message: '温馨提示',
        description: `退出成功`,
      });
    },


    *fetchListLoginAdminAccess({ payload, callback }, { call, put }) {
      const response = yield call(fetchListLoginAdminAccess, payload);
      if (!response) return;
      const {
        content: { accessList, bottomAccessList = [] },
      } = response;
      yield put({
        type: 'save',
        payload: {
          accessList,
          bottomAccessList,
        },
      });
      callback && callback(accessList);
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    findAccessId(state, { payload }) {
      const findIdByUrl = (data, url) => {
        for (let i = 0; i < data.length; i++) {
          if (data[i].accessUrl === url) {
            return data[i].id;
          }
          if (data[i].subAccessList) {
            const id = findIdByUrl(data[i].subAccessList, url);
            if (id) {
              return id;
            }
          }
        }
        return null; // 如果没有找到对应的accessUrl，则返回null
      };
      const { accessList } = state;
      const { pathname, query = {}, linkPush = 'push' } = payload;
      const { accessId } = query;
      const id = findIdByUrl(accessList, pathname);
      const { location } = history;
      history.push({
        pathname: pathname,
        query: {
          ...query,
          accessId: id || location.query.accessId,
        },
      });
      // if (linkPush === 'push') {

      // } else {
      //   const queryParams = Object.keys(query)
      //     .map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(query[key])}`)
      //     .join('&');
      // window.open(
      //   `/#${pathname}?accessId=${findIdByUrl(accessList, pathname) || accessId}&${queryParams}`,
      // );
      // }
      return {
        ...state,
      };
    },
  },
};
export default LoginForm;
